package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.jsp.PageContext;
import javax.sound.sampled.DataLine;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.PublicDao;
import com.hhit.collaborative_learning.entity.Blog;
import com.hhit.collaborative_learning.entity.BlogComment;
import com.hhit.collaborative_learning.entity.BlogCommentPage;
import com.hhit.collaborative_learning.entity.BlogExpert;
import com.hhit.collaborative_learning.entity.BlogPage;
import com.hhit.collaborative_learning.entity.BlogRight;
import com.hhit.collaborative_learning.entity.BlogRightPage;
import com.hhit.collaborative_learning.entity.Data;
import com.hhit.collaborative_learning.entity.DataComment;
import com.hhit.collaborative_learning.entity.DataCommentPage;
import com.hhit.collaborative_learning.entity.DataList;
import com.hhit.collaborative_learning.entity.DataPage;
import com.hhit.collaborative_learning.entity.Question;
import com.hhit.collaborative_learning.entity.QuestionAnswer;
import com.hhit.collaborative_learning.entity.QuestionComment;
import com.hhit.collaborative_learning.entity.QuestionPage;
import com.hhit.collaborative_learning.entity.SearchResult;
import com.hhit.collaborative_learning.entity.SearchResultPage;
import com.hhit.collaborative_learning.entity.User;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.Utils;
import com.mysql.jdbc.StringUtils;

public class PublicDaoImplMySql implements PublicDao {

	@Override
	public User validateLogin(String username, String password) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = DBAccess.getInstance().getConn();
			String sql = "select user.userId,userNickName,userIcon,diggedIds from user,blog_userinfo where user.userId=blog_userinfo.userId and userName=? and userPwd=?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, username);
			ps.setObject(2, password);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.userIcon = rs.getString("userIcon");
				user.userId = rs.getInt("userId");
				user.userNickName = rs.getString("userNickName");
				user.digged = rs.getString("diggedIds");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return user;
	}

	@Override
	public User register(String username, String password, String email,
			String usericon, String nickName) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = DBAccess.getInstance().getConn();
			String sql = "";
			if (StringUtils.isNullOrEmpty(usericon)) {
				sql = "insert into user(userName,userPwd,userEmail,userNickName) values(?,?,?,?)";
			} else {
				sql = "insert into user(userName,userPwd,userEmail,userNickName,userIcon) values(?,?,?,?,?)";
			}
			ps = conn.prepareStatement(sql);
			ps.setObject(1, username);
			ps.setObject(2, password);
			ps.setObject(3, email);
			ps.setObject(4, nickName);
			if (!StringUtils.isNullOrEmpty(usericon)) {
				ps.setObject(5, usericon);
			}
			ps.executeUpdate();
			ps.close();
			ps = conn.prepareStatement("select LAST_INSERT_ID()");
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.userId = rs.getInt(1);
				user.userNickName = nickName;
				user.userIcon = StringUtils.isNullOrEmpty(usericon) ? "default.jpg"
						: usericon;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return user;
	}

	@Override
	public boolean verityNameAndEmail(String username, String email) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			String sql = "select count(*) from user where userName=? and userEmail=?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, username);
			ps.setObject(2, email);
			rs = ps.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if (count == 1)
			return true;
		return false;
	}

	@Override
	public boolean findPWd(String username, String password, String email) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			String sql = "update user set userPwd=? where userName=? and userEmail=?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, password);
			ps.setObject(2, username);
			ps.setObject(3, email);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public int getCount(String sql) {
		int count = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return count;
	}

	@Override
	public BlogPage getBlog(String type, String currentPageStr, String orderBy) {
		if (StringUtils.isNullOrEmpty(Utils.sevenDaysAgo)) {
			Calendar now = Calendar.getInstance();
			Utils.getSomedaysAgo(now);
		}
		int pageSize = 10;
		String sql = "";
		if (StringUtils.isNullOrEmpty(type) || type.equals("all")) {
			sql = "select count(*) from blog where str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.sevenDaysAgo
					+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.currentTime + "' and isDelete=0 and state=1";
			type = "all";
		} else {
			sql = "select count(*) from blog where str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.sevenDaysAgo
					+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.currentTime
					+ "' and category='"
					+ type
					+ "' and isDelete=0 and state=1";
		}
		int rowCount = getCount(sql);
		int totalPage = rowCount % pageSize == 0 ? rowCount / pageSize
				: rowCount / pageSize + 1;
		int currentpage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentpage = Integer.parseInt(currentPageStr);
		}
		if (currentpage > totalPage) {
			currentpage = totalPage;
		}
		if (currentpage <= 0) {
			currentpage = 1;
		}
		if (StringUtils.isNullOrEmpty(orderBy)) {
			orderBy = "readNum";
		}
		if (type.equals("all")) {
			sql = "select blog.*,userNickName,userIcon "
					+ "from blog,user "
					+ "where blog.userId=user.userId and str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.sevenDaysAgo
					+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.currentTime + "' and isDelete=0 and state=1"
					+ " order by " + orderBy + " desc limit "
					+ (currentpage - 1) * pageSize + "," + pageSize;
		} else {
			sql = "select blog.*,userNickName,userIcon "
					+ "from blog,user "
					+ "where blog.userId=user.userId and str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.sevenDaysAgo
					+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.currentTime
					+ "' and isDelete=0 and state=1 and category='" + type
					+ "' order by " + orderBy + " desc limit "
					+ (currentpage - 1) * pageSize + "," + pageSize;
		}
		// System.out.println("sql="+sql);
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		BlogPage blogPage = new BlogPage();
		ArrayList<Blog> blogList = new ArrayList<Blog>();
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Blog blog = new Blog();
				blog.badNum = rs.getInt("badNum");
				blog.blogContent = rs.getString("blogContent");
				blog.blogDesc = rs.getString("blogDesc");
				blog.blogId = rs.getInt("blogId");
				blog.blogName = rs.getString("blogName");
				blog.category = rs.getString("category");
				blog.category_CN = rs.getString("category_CN");
				blog.commentNum = rs.getInt("commentNum");
				blog.goodNum = rs.getInt("goodNum");
				blog.readNum = rs.getInt("readNum");
				String tag = rs.getString("tag");
				if (!StringUtils.isNullOrEmpty(tag)) {
					blog.tag = tag.split(",");
				}
				blog.time = Utils.getRelativeTime(rs.getString("time"));
				blog.userIcon = rs.getString("userIcon");
				blog.userId = rs.getInt("userId");
				blog.userName = rs.getString("userNickName");
				blog.isRecommend = rs.getInt("isRecommend");
				blogList.add(blog);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		blogPage.list = blogList;
		blogPage.currentPage = currentpage;
		blogPage.totalPage = totalPage;
		blogPage.count = rowCount;
		return blogPage;
	}

	@Override
	public ArrayList<User> getRecomExpert(String type) {
		if (StringUtils.isNullOrEmpty(Utils.sevenDaysAgo)) {
			Calendar now = Calendar.getInstance();
			Utils.getSomedaysAgo(now);
		}
		ArrayList<User> experts = new ArrayList<User>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		if (StringUtils.isNullOrEmpty(type)) {
			sql = "select blog.userId,userIcon,userNickName,sum(readNum) total "
					+ "from user,blog_expert,blog "
					+ "where blog.userId=blog_expert.userId and blog_expert.userId=user.userId and isDelete=0 and state=1 and applyState=0"
					+ " group by blog_expert.userId order by total desc limit 0,12";
		} else {
			sql = "SELECT user.userId,userIcon,userNickName,count(blog.userId) total "
					+ "from blog_expert left join blog on blog_expert.userId=blog.userId and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.sevenDaysAgo
					+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.currentTime
					+ "' and isDelete=0 and state=1,user "
					+ "where blog_expert.category='"
					+ type
					+ "' and user.userId=blog_expert.userId and applyState=0 "
					+ "group by blog_expert.userId " + "order by total desc";
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.userId = rs.getInt("userId");
				user.userIcon = rs.getString("userIcon");
				user.userNickName = rs.getString("userNickName");
				experts.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return experts;
	}

	@Override
	public BlogPage getExpertBlog(String type, String currentPageStr) {
		BlogPage blogPage = new BlogPage();
		ArrayList<Blog> blogs = new ArrayList<Blog>();
		String sql = "";
		if (StringUtils.isNullOrEmpty(type)) {
			sql = "select count(*) "
					+ "from blog,blog_expert,user "
					+ "where blog.userId=blog_expert.userId and blog_expert.userId=user.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.expertSevenDaysAgo
					+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.expertCurrentTime + "'";
			type = "all";
		} else {
			sql = "select count(*) "
					+ "from blog,blog_expert,user "
					+ "where blog.category='"
					+ type
					+ "' and blog.userId=blog_expert.userId and blog_expert.userId=user.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.expertSevenDaysAgo
					+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.expertCurrentTime + "'";
		}
		int rowCount = getCount(sql);
		int pageSize = 10;
		int totalPage = rowCount % pageSize == 0 ? rowCount / pageSize
				: rowCount / pageSize + 1;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		if (currentPage > totalPage) {
			currentPage = totalPage;
		}
		if (currentPage <= 0) {
			currentPage = 1;
		}

		if (type.equals("all")) {
			sql = "select blog.*,userIcon,userNickName "
					+ "from blog,blog_expert,user "
					+ "where blog.userId=blog_expert.userId and blog_expert.userId=user.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.expertSevenDaysAgo
					+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.expertCurrentTime + "' "
					+ "order by blog.time desc limit " + pageSize
					* (currentPage - 1) + "," + pageSize;
		} else {
			sql = "select blog.*,userIcon,userNickName "
					+ "from blog,blog_expert,user "
					+ "where blog.category='"
					+ type
					+ "' and blog.userId=blog_expert.userId and blog_expert.userId=user.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
					+ Utils.expertSevenDaysAgo
					+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.expertCurrentTime + "' "
					+ "order by blog.time desc limit " + pageSize
					* (currentPage - 1) + "," + pageSize;
		}
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Blog blog = new Blog();
				blog.badNum = rs.getInt("badNum");
				blog.blogContent = rs.getString("blogContent");
				blog.blogDesc = blog.blogContent.substring(0, 50);
				blog.blogId = rs.getInt("blogId");
				blog.blogName = rs.getString("blogName");
				blog.category = rs.getString("category");
				blog.category_CN = rs.getString("category_CN");
				blog.commentNum = rs.getInt("commentNum");
				blog.goodNum = rs.getInt("goodNum");
				blog.readNum = rs.getInt("readNum");
				String tag = rs.getString("tag");
				if (!StringUtils.isNullOrEmpty(tag)) {
					blog.tag = tag.split(",");
				}
				blog.time = Utils.getRelativeTime(rs.getString("time"));
				blog.userIcon = rs.getString("userIcon");
				blog.userId = rs.getInt("userId");
				blog.userName = rs.getString("userNickName");
				blogs.add(blog);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		blogPage.list = blogs;
		blogPage.currentPage = currentPage;
		blogPage.totalPage = totalPage;
		blogPage.count = rowCount;
		return blogPage;
	}

	@Override
	public User getUserInfo(int userId) {
		synchronized (PublicDaoImplMySql.class) {
			User user = new User();
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;

			String sql = "select A.*,B.*,C.*,D.*,E.* from "
					+ "("
					+ "select ranking,userIcon,userNickName,userPoint,isExpert from "
					+ "("
					+ "select M.*,(@rowno:=@rowno+1) as ranking from "
					+ "(select blog.userId,sum(readNum) num,userIcon,userNickName,userPoint,isExpert from blog,user,blog_userinfo where blog.userId=user.userId and blog_userinfo.userId=user.userId and isDelete=0 and state=1 group by blog.userId order by num desc) M,"
					+ "(select (@rowno:=0)) N" + ") T " + "where userId="
					+ userId
					+ ") A,"
					+ "("
					+ "select sum(readNum) totalReadNum,sum(commentNum) totalCommentNum "
					+ "from blog "
					+ "where userId="
					+ userId
					+ " and isDelete=0 and state=1 "
					+ "group by userId"
					+ ") B,"
					+ "("
					+ "select count(*) originalNum "
					+ "from blog "
					+ "where userId="
					+ userId
					+ " and type=1 and isDelete=0 and state=1"
					+ ") C,"
					+ "("
					+ "select count(*) repostNum "
					+ "from blog "
					+ "where userId="
					+ userId
					+ " and type=2 and isDelete=0 and state=1"
					+ ") D,"
					+ "("
					+ "select count(*) translatedNum "
					+ "from blog "
					+ "where userId="
					+ userId
					+ " and type=3 and isDelete=0 and state=1" + ") E";

			try {
				conn = DBAccess.getInstance().getConn();
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				if (rs.next()) {
					user.userId = userId;
					user.userIcon = rs.getString("userIcon");
					user.userNickName = rs.getString("userNickName");
					user.userPoint = rs.getInt("userPoint");
					user.originalNum = rs.getInt("originalNum");
					user.ranking = rs.getInt("ranking");
					user.repostNum = rs.getInt("repostNum");
					user.totalCommentNum = rs.getInt("totalCommentNum");
					user.totalReadNum = rs.getInt("totalReadNum");
					user.translatedNum = rs.getInt("translatedNum");
					user.isExpert = rs.getInt("isExpert");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBAccess.getInstance().close(conn, ps, rs);
			}
			return user;
		}
	}

	@Override
	public HashMap<String, Object> getBlogById(int blogId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		Blog blog = new Blog();
		Blog previousBlog = null;
		Blog nextBlog = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from blog where blogId=" + blogId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				blog.badNum = rs.getInt("badNum");
				blog.blogContent = rs.getString("blogContent");
				blog.blogDesc = rs.getString("blogDesc");
				blog.blogId = rs.getInt("blogId");
				blog.blogName = rs.getString("blogName");
				blog.category = rs.getString("category");
				blog.category_CN = rs.getString("category_CN");
				blog.commentNum = rs.getInt("commentNum");
				blog.canComment = rs.getInt("canComment");
				blog.goodNum = rs.getInt("goodNum");
				blog.readNum = rs.getInt("readNum");
				String tag = rs.getString("tag");
				if (!StringUtils.isNullOrEmpty(tag)) {
					blog.tag = tag.split(",");
				}
				blog.isDelete = rs.getInt("isDelete");
				blog.time = rs.getString("time");
				blog.userId = rs.getInt("userId");
				blog.type = rs.getInt("type");
			}
			map.put("blog", blog);

			sql = "select blogName,blogId from blog where time>'" + blog.time
					+ "' and userId=" + blog.userId
					+ " and isDelete=0 and state=1 order by time limit 1;";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				nextBlog = new Blog();
				nextBlog.blogName = rs.getString("blogName");
				nextBlog.blogId = rs.getInt("blogId");
			}
			map.put("nextBlog", nextBlog);

			sql = "select blogName,blogId from blog where time<'" + blog.time
					+ "' and userId=" + blog.userId
					+ " and isDelete=0 and state=1 order by time desc limit 1;";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				previousBlog = new Blog();
				previousBlog.blogName = rs.getString("blogName");
				previousBlog.blogId = rs.getInt("blogId");
			}
			map.put("previousBlog", previousBlog);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return map;
	}

	@Override
	public BlogPage getBlogByUserId(int userId, String currentPageStr) {
		BlogPage blogPage = new BlogPage();
		ArrayList<Blog> blogs = new ArrayList<Blog>();
		String sql = "select count(*) from blog where userId=" + userId
				+ " and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
				+ Utils.personalCurrentTime + "' and isDelete=0 and state=1";
		int totalRow = getCount(sql);
		int pageSize = 15;
		int totalPage = totalRow % pageSize == 0 ? totalRow / pageSize
				: totalRow / pageSize + 1;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		sql = "select * from blog where userId="
				+ userId
				+ " and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
				+ Utils.personalCurrentTime
				+ "' and isDelete=0 and state=1 order by hasTop desc,time desc limit "
				+ (currentPage - 1) * pageSize + "," + pageSize;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Blog blog = new Blog();
				blog.blogId = rs.getInt("blogId");
				blog.blogContent = rs.getString("blogContent");
				blog.blogDesc = rs.getString("blogDesc");
				blog.blogName = rs.getString("blogName");
				blog.time = rs.getString("time");
				blog.readNum = rs.getInt("readNum");
				blog.commentNum = rs.getInt("commentNum");
				blog.type = rs.getInt("type");
				blog.isTop = rs.getInt("hasTop");
				blog.canComment = rs.getInt("canComment");
				blog.state = rs.getInt("state");
				blogs.add(blog);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		blogPage.count = totalRow;
		blogPage.currentPage = currentPage;
		blogPage.list = blogs;
		blogPage.totalPage = totalPage;
		return blogPage;
	}

	@Override
	public String saveBlog(boolean isFirst, String blogName,
			String blogContent, String blogDesc, String category,
			String canComment, String isTop, String tag, String userId,
			String isDelete, String blogId, String type) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int flag = 0;
		String result = "";
		String sql = "";
		if (isFirst) {
			sql = "insert into blog(blogName,blogContent,blogDesc,category,category_CN,userId,tag,time,type,isDelete,canComment,isTop) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		} else {
			sql = "update blog set blogName=?,blogContent=?,blogDesc=?,category=?,category_CN=?,userId=?,tag=?,time=?,type=?,isDelete=?,canComment=?,isTop=? where blogId=?";
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, blogName);
			ps.setObject(2, blogContent);
			ps.setObject(3, blogDesc);
			ps.setObject(4, category);
			ps.setObject(5, Utils.getRelativeName(category));
			ps.setObject(6, Integer.parseInt(userId));
			ps.setObject(7, tag);
			ps.setObject(8, Utils.getCurrentTime());
			ps.setObject(9, Integer.parseInt(type));
			ps.setObject(10, Integer.parseInt(isDelete));
			ps.setObject(11, Integer.parseInt(canComment));
			ps.setObject(12, Integer.parseInt(isTop));
			if (!isFirst) {
				ps.setObject(13, Integer.parseInt(blogId));
			}
			flag = ps.executeUpdate();

			if (flag == 1) {
				result += "true;";
				int flagId = 0;
				if (isFirst) {
					ps.close();
					ps = conn.prepareStatement("select LAST_INSERT_ID()");
					rs = ps.executeQuery();
					if (rs.next()) {
						result += rs.getInt(1);
						flagId = rs.getInt(1);
					}
				}
				if (isDelete.equals("0")) {
					ps.close();
					ps = conn
							.prepareStatement("update user set userPoint=userPoint+20 where userId="
									+ userId);
					ps.executeUpdate();
				}
				if (isDelete.equals("0") && isTop.equals("1")) {
					ps.close();
					ps = conn
							.prepareStatement("insert into blog_top(userId,blogId,time) values(?,?,?)");
					ps.setObject(1, userId);
					if (isFirst) {
						ps.setObject(2, flagId);
					} else {
						ps.setObject(2, blogId);
					}
					ps.setObject(3, Utils.getCurrentTime());
					ps.executeUpdate();
				}
			} else {
				result += "false;";
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return result;
	}

	@Override
	public BlogPage getBlogByUserIdTypeIsDelete(int userId, String type,
			String isDelete, String currentPageStr) {
		BlogPage blogPage = new BlogPage();
		ArrayList<Blog> blogList = new ArrayList<Blog>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		if (StringUtils.isNullOrEmpty(isDelete)) {
			isDelete = "0";
		}
		if (type.equals("0")) {
			sql += "select count(*) " + "from blog where userId=" + userId
					+ " and isDelete=" + Integer.parseInt(isDelete);
		} else {
			sql += "select count(*) " + "from blog where userId=" + userId
					+ " and isDelete=" + Integer.parseInt(isDelete)
					+ " and type=" + Integer.parseInt(type);
		}
		int totalRow = getCount(sql);
		int pageSize = 10;
		int totalPage = totalRow % pageSize == 0 ? totalRow / pageSize
				: totalRow / pageSize + 1;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}

		if (StringUtils.isNullOrEmpty(type) || type.equals("0")) {
			sql = "select blogId,blogName,time,readNum,commentNum,canComment,state,type "
					+ "from blog where userId="
					+ userId
					+ " and isDelete="
					+ Integer.parseInt(isDelete)
					+ ""
					+ " order by time desc limit "
					+ (currentPage - 1)
					* pageSize + "," + pageSize;
		} else {
			sql = "select blogId,blogName,time,readNum,commentNum,canComment,state,type "
					+ "from blog where userId="
					+ userId
					+ " and isDelete="
					+ Integer.parseInt(isDelete)
					+ " and type="
					+ Integer.parseInt(type)
					+ " order by time desc limit "
					+ (currentPage - 1) * pageSize + "," + pageSize;
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Blog blog = new Blog();
				blog.blogId = rs.getInt("blogId");
				blog.blogName = rs.getString("blogName");
				blog.time = rs.getString("time");
				blog.readNum = rs.getInt("readNum");
				blog.commentNum = rs.getInt("commentNum");
				blog.canComment = rs.getInt("canComment");
				blog.state = rs.getInt("state");
				blog.type = rs.getInt("type");
				blogList.add(blog);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		blogPage.count = totalRow;
		blogPage.currentPage = currentPage;
		blogPage.list = blogList;
		blogPage.totalPage = totalPage;
		return blogPage;
	}

	@Override
	public boolean manageBlog(String action, int blogId) {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "";
		int flag = 0;
		if (action.equals("0")) {
			sql += "delete from blog where blogId=" + blogId;
		} else if (action.equals("2")) {
			sql += "update blog set isDelete=1 where blogId=" + blogId;
		} else if (action.equals("4")) {
			sql += "update blog set canComment=1 where blogId=" + blogId;
		} else if (action.equals("5")) {
			sql += "update blog set canComment=0 where blogId=" + blogId;
		} else if (action.equals("7")) {
			sql += "update blog set readNum=readNum+1 where blogId=" + blogId;
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	@Override
	public boolean applyExpert(int userId, String realname, String realicon,
			String mobile, String category, String tag, String qq,
			String address, String description) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "insert into blog_expert(userId,realName,realIcon,mobile,category,goodAt,qq,address,brief,time,applyState,conditionOne,conditionTwo,conditionThree) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, userId);
			ps.setObject(2, realname);
			ps.setObject(3, realicon);
			ps.setObject(4, mobile);
			ps.setObject(5, category);
			ps.setObject(6, tag);
			ps.setObject(7, qq);
			ps.setObject(8, address);
			ps.setObject(9, description);
			ps.setObject(10, Utils.getCurrentTime());
			ps.setObject(11, 1);
			ps.setObject(12, 2);
			ps.setObject(13, 2);
			ps.setObject(14, 2);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	@Override
	public int getExpertApplyState(int userId) {
		int applyState = -1;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select applyState from blog_expert where userId="
				+ userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				applyState = rs.getInt("applyState");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return applyState;
	}

	@Override
	public BlogExpert getExpertApplyResult(int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		BlogExpert expert = new BlogExpert();
		String sql = "select realName,time,applyState,conditionOne,conditionTwo,conditionThree from blog_expert where userId="
				+ userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				expert.time = rs.getString("time");
				expert.realName = rs.getString("realName");
				expert.conditionOne = rs.getInt("conditionOne");
				expert.conditionTwo = rs.getInt("conditionTwo");
				expert.conditionThree = rs.getInt("conditionThree");
				expert.applyState = rs.getInt("applyState");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return expert;
	}

	@Override
	public void deleteFailApplyResult(int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "delete from blog_expert where applyState=2 and userId="
				+ userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
	}

	@Override
	public String getBlog(int blogId) {
		JSONObject obj = new JSONObject();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int userId = 0;
		String sql = "select blog.*,userPoint,isExpert "
				+ "from blog,user,blog_userinfo "
				+ "where blog.userId=user.userId and blog.userId=blog_userinfo.userId and blogId="
				+ blogId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				obj.put("articleId", rs.getInt("blogId") + "");
				int isDelete = rs.getInt("isDelete");
				if (isDelete == 2) {
					obj.put("isDraft", "True");
				}
				obj.put("desc", rs.getString("blogDesc"));
				userId = rs.getInt("userId");
				obj.put("content", rs.getString("blogContent"));
				obj.put("title", rs.getString("blogName"));
				obj.put("type", rs.getInt("type") + "");
				obj.put("tag2", rs.getString("tag"));
				obj.put("channel", rs.getString("category"));
				obj.put("comment", rs.getInt("canComment") + "");
				int isTop = rs.getInt("isTop");
				int hasTop = rs.getInt("hasTop");
				if (hasTop == 1) {
					obj.put("hasTop", "True");
				}
				if (isTop == 1) {
					obj.put("toHome", "True");
				}
				int isExpert = rs.getInt("isExpert");
				if (isExpert == 0) {
					obj.put("expert", "False");
				}
				obj.put("tohome_max_cnt", "5");
				int point = rs.getInt("userPoint");
				if (point < 100) {
					obj.put("point", "False");
				}
				obj.put("max_point", "100");
			}

			sql = "select A.*,B.* from (select count(*) sum1 from blog_top where blogId="
					+ blogId
					+ ") A,(select count(*) sum2 from blog_top where userId="
					+ userId
					+ " and time like '"
					+ Utils.getCurrentTime().substring(0, 10) + "%')B;";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				obj.put("tohome_cnt", rs.getInt("sum2") + "");
				int sum1 = rs.getInt("sum1");
				if (sum1 != 0) {
					obj.put("unhome", "True");
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return obj.toString();
	}

	@Override
	public String setTop(int blogId, int userId, boolean hasRight) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = 0;
		String result = "";
		try {
			conn = DBAccess.getInstance().getConn();
			if (hasRight) {
				ps = conn
						.prepareStatement("update blog set isTop=1,hasTop=1 where blogId="
								+ blogId);
				ps.addBatch();
				ps.addBatch("insert into blog_top(userId,blogId,time) values("
						+ userId + "," + blogId + ",'" + Utils.getCurrentTime()
						+ "')");
				ps.executeBatch();
				result += "置顶成功";
			} else {
				ps = conn
						.prepareStatement("select userPoint from user where userId="
								+ userId);
				rs = ps.executeQuery();
				int point = 0;
				if (rs.next()) {
					point = rs.getInt("userPoint");
				}
				if (point >= 100) {
					String sql = "select count(*) total from blog_top where blogId="
							+ blogId;
					ps = conn.prepareStatement(sql);
					rs = ps.executeQuery();
					if (rs.next()) {
						count = rs.getInt("total");
					}

					if (count == 0) {
						ps.addBatch("update blog set isTop=1,hasTop=1 where blogId="
								+ blogId);
						ps.addBatch("insert into blog_top(userId,blogId,time) values("
								+ userId
								+ ","
								+ blogId
								+ ",'"
								+ Utils.getCurrentTime() + "')");
						ps.executeBatch();
						result += "置顶成功";
					} else {
						result += "该博客已置顶过，无法再次置顶";
					}
				} else {
					result += "积分不够，无法置顶";
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "置顶失败";
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return result;
	}

	@Override
	public void deleteCompletely(int blogId) {
		Connection conn = null;
		Statement ps = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			ps.addBatch("delete from blog_comment where blogId=" + blogId);
			ps.addBatch("delete from blog_top where blogId=" + blogId);
			ps.addBatch("delete from blog where blogId=" + blogId);
			ps.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
	}

	@Override
	public HashMap<String, Object> getAttention(int userId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		StringBuilder sb3 = new StringBuilder("");
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select attention from blog_userinfo where userId="
							+ userId);
			rs = ps.executeQuery();
			String attention = "";
			if (rs.next()) {
				attention += rs.getString("attention");
			}
			ps.close();
			if (StringUtils.isNullOrEmpty(attention)) {
				map.put("myAttention", 0);
				map.put("attentionEachOther", 0);

				ps = conn
						.prepareStatement("select count(*) total from blog_userinfo where attention like '%"
								+ userId + "%'");
				rs = ps.executeQuery();
				if (rs.next()) {
					map.put("attentionMe", rs.getInt("total"));
				}
			} else {
				String[] users = attention.split(",");
				map.put("myAttention", users.length);
				StringBuilder sb = new StringBuilder("");
				sb.append("select A.*,B.* from (");
				StringBuilder sb2 = new StringBuilder("");
				sb2.append("select count(*) total1 from blog_userinfo where attention like '%"
						+ userId + "%' and userId in (");
				for (String user : users) {
					sb2.append(user + ",");
					sb3.append(user + ",");
				}
				sb.append(sb2.substring(0, sb2.length() - 1)
						+ "))A,(select count(*) total2 from blog_userinfo where attention like '%"
						+ userId + "%')B");
				ps = conn.prepareStatement(sb.toString());
				rs = ps.executeQuery();
				if (rs.next()) {
					map.put("attentionEachOther", rs.getInt("total1"));
					map.put("attentionMe", rs.getInt("total2"));
				}
				map.put("attentionUsers", sb3.toString());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if (!StringUtils.isNullOrEmpty(sb3.toString())) {
			HashMap<String, Object> map1 = getAttention(sb3.toString(), 0,
					userId, "");
			map.put("list", map1.get("list"));
			map.put("currentPage", map1.get("currentPage"));
		}
		return map;
	}

	// 1 关注我的 2 互相关注的 0 我关注的
	public HashMap<String, Object> getAttention(String myAttention, int type,
			int userId, String currentPageStr) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		int pageSize = 10;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		if (type == 1) {
			sql += "select user.userId,userIcon,userNickName,userBrief from blog_userinfo,user where blog_userinfo.userId=user.userId and attention like '%"
					+ userId
					+ "%' limit "
					+ (currentPage - 1)
					* pageSize
					+ ","
					+ pageSize;
		} else if (type == 2) {
			sql += "select user.userId,userIcon,userNickName,userBrief from blog_userinfo,user where blog_userinfo.userId=user.userId and attention like '%"
					+ userId
					+ "%' and blog_userinfo.userId in("
					+ myAttention.substring(0, myAttention.length() - 1)
					+ ") limit "
					+ (currentPage - 1)
					* pageSize
					+ ","
					+ pageSize;
		} else {
			sql += "select user.userId,userIcon,userNickName,userBrief from blog_userinfo,user where blog_userinfo.userId=user.userId and blog_userinfo.userId in("
					+ myAttention.substring(0, myAttention.length() - 1)
					+ ") limit "
					+ (currentPage - 1)
					* pageSize
					+ ","
					+ pageSize;
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			ArrayList<User> list = new ArrayList<User>();
			while (rs.next()) {
				User user = new User();
				user.userIcon = rs.getString("userIcon");
				user.userId = rs.getInt("userId");
				user.userNickName = rs.getString("userNickName");
				user.userBrief = rs.getString("userBrief");
				list.add(user);
			}
			map.put("list", list);
			map.put("currentPage", currentPage);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return map;
	}

	@Override
	public boolean cancelFocus(int userId, String c_userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int flag = 0;
		String attention = "";
		String sql = "select attention from blog_userinfo where userId="
				+ userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				attention += rs.getString("attention");
			}
			ps.close();

			attention = attention.replace(c_userId, "");
			ps = conn.prepareStatement("update blog_userinfo set attention='"
					+ attention + "' where userId=" + userId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public boolean addFocus(int userId, String a_userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "update blog_userinfo set attention=CONCAT(attention,'"
				+ a_userId + ",') where userId=" + userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	@Override
	public String addFocus2(int userId, String a_userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String attention = "";
		int flag = 0;
		String result = "";
		try {
			conn = DBAccess.getInstance().getConn();

			if (judgeFocus(userId, a_userId)) {

			} else {
				ps = conn
						.prepareStatement("update blog_userinfo set attention=CONCAT(attention,'"
								+ a_userId + ",') where userId=" + userId);
				flag = ps.executeUpdate();
			}
			if (flag == 0) {
				result += "已经关注！";
			} else if (flag == 1) {
				result += "关注成功!";
			}
		} catch (SQLException e) {
			e.printStackTrace();
			result += "关注失败";
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return result;
	}

	@Override
	public boolean judgeFocus(int userId, String a_userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String attention = "";
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select attention from blog_userinfo where userId="
							+ userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				attention = rs.getString("attention");
			}
			if (!StringUtils.isNullOrEmpty(attention)) {
				String[] attentions = attention.split(",");
				for (String a : attentions) {
					if (a.equals(a_userId)) {
						flag = -1;
						break;
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == -1)
			return true;
		return false;
	}

	@Override
	public String getComment(int blogId, String currentPageStr) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		JSONArray arr = new JSONArray();
		int pageSize = 200;
		int count = 0;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select blog_comment.*,userNickName,userIcon from blog_comment,user where blog_comment.comUserId=user.userId and blogId="
							+ blogId
							+ " and isDelete=0 order by time desc limit "
							+ (currentPage - 1) * pageSize + "," + pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				BlogComment comment = new BlogComment();
				comment.blogId = rs.getInt("blogId");
				comment.comContent = rs.getString("comContent");
				comment.comId = rs.getInt("comId");
				comment.comUserId = rs.getInt("comUserId");
				comment.parentId = rs.getInt("parentComId");
				comment.time = Utils.getRelativeTime(rs.getString("time"));
				comment.comUserName = rs.getString("userNickName");
				comment.comUserIcon = rs.getString("userIcon");
				if (comment.parentId == 0) {
					count++;
				}
				arr.add(comment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		json.put("list", arr);
		json.put("total", count);
		json.put("totalRow", arr.size());
		json.put("currentPage", currentPage);
		return json.toString();
	}

	@Override
	public boolean saveComment(int userId, int blogId, String content,
			int parentId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("insert into blog_comment(comUserId,comContent,blogId,parentComId,time,isDelete)values(?,?,?,?,?,?)");
			ps.setObject(1, userId);
			ps.setObject(2, content);
			ps.setObject(3, blogId);
			ps.setObject(4, parentId);
			ps.setObject(5, Utils.getCurrentTime());
			ps.setObject(6, 0);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	@Override
	public boolean deleteComment(int comId, int blogId) {
		Connection conn = null;
		Statement ps = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			ps.addBatch("update blog_comment set parentComId=0 where parentComId="
					+ comId);
			ps.addBatch("update blog_comment set isDelete=1 where comId="
					+ comId);
			ps.addBatch("update blog set commentNum=commentNum-1 where blogId="
					+ blogId);
			ps.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return true;
	}

	@Override
	public boolean digBlog(int userId, int blogId, String action) {
		Connection conn = null;
		Statement ps = null;
		ResultSet rs = null;
		String dig = "";
		int flag = 0;
		String sql = "select diggedIds from blog_userinfo where userId="
				+ userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			rs = ps.executeQuery(sql);
			if (rs.next()) {
				dig = rs.getString("diggedIds");
			}
			ps.close();
			ps = conn.createStatement();
			if (StringUtils.isNullOrEmpty(dig)) {
				if (action.equals("up")) {
					ps.addBatch("update blog set goodNum=goodNum+1 where blogId="
							+ blogId);
					ps.addBatch("update blog_userinfo set diggedIds=CONCAT(diggedIds,'"
							+ blogId + ",') where userId=" + userId);
				} else {
					ps.addBatch("update blog set badNum=badNum+1 where blogId="
							+ blogId);
					ps.addBatch("update blog_userinfo set diggedIds=CONCAT(diggedIds,'"
							+ blogId + ",') where userId=" + userId);
				}
				ps.executeBatch();
			} else {
				String[] digs = dig.split(",");
				for (String d : digs) {
					if (d.equals("" + blogId)) {
						flag = -1;
						break;
					}
				}
				if (flag == 0) {
					if (action.equals("up")) {
						ps.addBatch("update blog set goodNum=goodNum+1 where blogId="
								+ blogId);
						ps.addBatch("update blog_userinfo set diggedIds=CONCAT(diggedIds,'"
								+ blogId + ",') where userId=" + userId);
					} else {
						ps.addBatch("update blog set badNum=badNum+1 where blogId="
								+ blogId);
						ps.addBatch("update blog_userinfo set diggedIds=CONCAT(diggedIds,'"
								+ blogId + ",') where userId=" + userId);
					}
					ps.executeBatch();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if (flag == -1)
			return false;
		return true;
	}

	@Override
	public BlogCommentPage getCommentByPage(int userId, String type,
			String currentPageStr) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		if (type.equals("in")) {
			sql = "select count(*) from blog_comment where isDelete=0 and str_to_date(blog_comment.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.commentCurrentTime
					+ "' and blog_comment.blogId in (select blogId from blog where userId = "
					+ userId + ")";
		} else {
			sql = "select count(*) from blog_comment where isDelete=0 and str_to_date(blog_comment.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.commentCurrentTime + "' and comUserId=" + userId;
		}
		int count = getCount(sql);
		int pageSize = 10;
		int totalPage = count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1;
		int currentPage = 1;
		ArrayList<BlogComment> list = new ArrayList<BlogComment>();
		BlogCommentPage page = new BlogCommentPage();
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		if (count == 0) {
			return page;
		}
		if (type.equals("in")) {
			sql = "select a.time,a.comId,a.blogId,a.comContent,a.comUserId,b.blogName,b.userId,c.userNickName from blog_comment a,blog b,user c where a.blogId=b.blogId and a.comUserId=c.userId and str_to_date(a.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.commentCurrentTime
					+ "' and a.isDelete=0 and a.blogId in (select blogId from blog where userId = "
					+ userId
					+ ") order by time desc limit "
					+ (currentPage - 1) * pageSize + "," + pageSize;
		} else {
			sql = "select a.time,a.comId,a.blogId,a.comContent,a.comUserId,b.blogName,b.userId,c.userNickName from blog_comment a,blog b,user c where a.blogId=b.blogId and a.comUserId=c.userId and str_to_date(a.time,'%Y-%m-%d %H:%i:%s')<='"
					+ Utils.commentCurrentTime
					+ "' and a.isDelete=0 and a.comUserId = "
					+ userId
					+ " order by time desc limit "
					+ (currentPage - 1)
					* pageSize + "," + pageSize;
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				BlogComment comment = new BlogComment();
				comment.blogId = rs.getInt("blogId");
				comment.blogName = rs.getString("blogName");
				comment.comContent = rs.getString("comContent");
				comment.comId = rs.getInt("comId");
				comment.comUserName = rs.getString("userNickName");
				comment.comUserId = rs.getInt("comUserId");
				comment.blogUserId = rs.getInt("userId");
				comment.time = rs.getString("time");
				list.add(comment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count = count;
		page.currentPage = currentPage;
		page.list = list;
		page.totalPage = totalPage;
		return page;
	}

	@Override
	public int getUserIdByUserNickName(String userNickName) {
		int userId = 0;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select userId from user where userNickName=?");
			ps.setObject(1, userNickName);
			rs = ps.executeQuery();
			if (rs.next()) {
				userId = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return userId;
	}

	@Override
	public String getBlogRight(int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String right = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select blogRight from blog_userinfo where userId="
							+ userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				right = rs.getString("blogRight");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return right;
	}

	@Override
	public BlogRightPage getBlogRightPage(String category, String type,
			String choose, String currentPageStr) {
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		int pageSize = 10;
		String sql1 = "", sql2 = "";
		if (type.equals("blog")) {
			if (choose.equals("0")) {
				sql1 += "select count(*) from blog where category='"
						+ category
						+ "' and state=1 and isDelete=0 and str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
						+ Utils.blogRightOneDaysAge
						+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
						+ Utils.blogRightCurrentTime + "'";
				sql2 += "select blogId,blogName,blog.userId,blog.type, user.userNickName,blog.time from blog,user where category='"
						+ category
						+ "' and state=1 and isDelete=0 and blog.userId=user.userId and str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
						+ Utils.blogRightOneDaysAge
						+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
						+ Utils.blogRightCurrentTime
						+ "' order by time desc limit "
						+ (currentPage - 1)
						* pageSize + "," + pageSize;
			} else {
				sql1 += "select count(*) "
						+ "from blog,blog_expert "
						+ "where blog.category='"
						+ category
						+ "' and blog.userId=blog_expert.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
						+ Utils.blogRightOneDaysAge
						+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
						+ Utils.blogRightCurrentTime + "'";
				sql2 += "select blogId,blogName,blog.userId,blog.type,user.userNickName,blog.time "
						+ "from blog,blog_expert,user "
						+ "where blog.category='"
						+ category
						+ "' and blog.userId=blog_expert.userId and blog_expert.userId=user.userId and applyState=0 and isDelete=0 and state=1 and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')>='"
						+ Utils.blogRightOneDaysAge
						+ "' and str_to_date(blog.time,'%Y-%m-%d %H:%i:%s')<='"
						+ Utils.blogRightCurrentTime
						+ "' order by blog.time desc limit "
						+ (currentPage - 1) * pageSize + "," + pageSize;
			}
		} else {
			sql1 += "select count(*) from blog_expert where applyState=1 and category='"
					+ category + "'";
			sql2 += "select user.userId,userNickName,time from blog_expert,user "
					+ "where blog_expert.userId=user.userId and applyState=1 and category='"
					+ category
					+ "' limit "
					+ (currentPage - 1)
					* pageSize
					+ "," + pageSize;
		}
		// System.out.println(sql2);
		int count = getCount(sql1);
		int totalPage = count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1;

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		BlogRightPage page = new BlogRightPage();
		ArrayList<BlogRight> list = new ArrayList<BlogRight>();
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql2);
			rs = ps.executeQuery();
			while (rs.next()) {
				BlogRight right = new BlogRight();
				if (type.equals("blog")) {
					right.blogId = rs.getInt("blogId");
					right.blogName = rs.getString("blogName");
					right.time = rs.getString("time");
					right.userId = rs.getInt("userId");
					right.userName = rs.getString("userNickName");
					right.blogType = rs.getInt("type");
				} else {
					right.userId = rs.getInt("userId");
					right.userName = rs.getString("userNickName");
					right.time = rs.getString("time");
				}
				list.add(right);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count = count;
		page.currentPage = currentPage;
		page.list = list;
		page.totalPage = totalPage;
		return page;
	}

	@Override
	public String setPrime(int blogId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("update blog set isPrime=1 where blogId="
							+ blogId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return "设置成功";
		return "设置失败";
	}

	@Override
	public boolean examine(String type, int userId, String time) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		int count = 0;
		boolean isOk = false;
		try {
			conn = DBAccess.getInstance().getConn();
			sql += "select count(*) from blog where userId=" + userId
					+ " and isDelete=0 and state=1 and type=1";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
			ps.close();
			if (count >= 20) {
				if (type.equals("1")) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					Date applyTime = sdf.parse(time);
					Calendar cal = Calendar.getInstance();
					cal.setTime(applyTime);
					cal.add(Calendar.DATE, -30);
					String monthAgo = sdf.format(cal.getTime());
					sql = "select count(*) from blog where user=" + userId
							+ " and str_to_date(time,'%Y-%m-%d %H:%i:%s')>='"
							+ monthAgo
							+ "' and str_to_date(time,'%Y-%m-%d %H:%i:%s')<='"
							+ time + "' and isDelete=0 and state=1 and type=1";
					ps = conn.prepareStatement(sql);
					rs = ps.executeQuery();
					if (rs.next()) {
						count = rs.getInt(1);
					}
					if (count >= 1) {
						isOk = true;
					}
				} else {
					sql = "select sum(readNum) from blog where isDelete=0 and state=1 and userId="
							+ userId + " group by userId";
					ps = conn.prepareStatement(sql);
					rs = ps.executeQuery();
					if (rs.next()) {
						count = rs.getInt(1);
						System.out.println(count);
					}
					if (count >= 50000) {
						isOk = true;
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return isOk;
	}

	@Override
	public boolean submitExamine(int con1, int con2, int con3, int userId,
			int applyState) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "update blog_expert set conditionOne=" + con1
				+ ",conditionTwo=" + con2 + ",conditionThree=" + con3
				+ ",applyState=" + applyState + " where userId=" + userId;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	@Override
	public QuestionPage getQuestion(String type, String orderby,
			String currentPageStr) {
		// type 1:全部 2悬赏 3待回答 4已解决
		// orderby time questionAnswerNum
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int pageSize = 10;
		int currentPage = 1;
		QuestionPage page = new QuestionPage();
		ArrayList<Question> list = new ArrayList<Question>();
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		String sql1 = "select count(*) from question";
		String sql2 = "select q.*,u.userNickName from question q,user u where q.questionUserId=u.userId ";
		if (type.equals("1")) {
		} else if (type.equals("2")) {
			sql1 += " where questionPoint>0 and state=0";
			sql2 += "and questionPoint>0 and state=0 ";
		} else if (type.equals("3")) {
			sql1 += " where state=0";
			sql2 += "and state=0 ";
		} else if (type.equals("4")) {
			sql1 += " where state=1";
			sql2 += "and state=1 ";
		}
		sql2 += "order by " + orderby + " desc limit " + (currentPage - 1)
				* pageSize + "," + pageSize;
		int count = getCount(sql1);
		int totalPage = count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql2);
			rs = ps.executeQuery();
			while (rs.next()) {
				Question q = new Question();
				q.questionAnswerNum = rs.getInt("questionAnswerNum");
				q.questionContent = rs.getString("questionContent");
				String desc = rs.getString("questionContent");
				desc = desc.replaceAll("<[^>]*>", "");
				if (desc.length() >= 100) {
					q.questionDesc = desc.substring(0, 100);
				} else {
					q.questionDesc = desc;
				}
				q.questionDigged = rs.getInt("questionDigged");
				q.questionId = rs.getInt("questionId");
				String tag = rs.getString("questionTag");
				q.questionTag = tag.split(",");
				q.questionPoint = rs.getInt("questionPoint");
				q.questionTime = rs.getString("questionTime");
				q.questionTitle = rs.getString("questionTitle");
				q.questionUserId = rs.getInt("questionUserId");
				q.questionUserName = rs.getString("userNickName");
				q.state = rs.getInt("state");
				list.add(q);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count = count;
		page.currentPage = currentPage;
		page.list = list;
		page.totalPage = totalPage;
		return page;
	}

	@Override
	public boolean submitQuestion(String title, String content, String tag,
			String point, int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "insert into question(questionTitle,questionContent,questionTag,questionUserId,questionTime,questionPoint) values(?,?,?,?,?,?)";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, title);
			ps.setObject(2, content);
			ps.setObject(3, tag);
			ps.setObject(4, userId);
			ps.setObject(5, Utils.getCurrentTime());
			if (StringUtils.isNullOrEmpty(point)) {
				ps.setObject(6, 0);
			} else {
				ps.setObject(6, Integer.parseInt(point));
			}
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1) {
			return true;
		}
		return false;
	}

	@Override
	public QuestionPage getMyQuestion(String type, int userId,
			String currentPageStr) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql1 = "";
		String sql2 = "";
		QuestionPage page = new QuestionPage();
		String sql3 = "";
		int pageSize = 10;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		if (type.equals("in")) {
			sql1 = "select count(*) from question where questionUserId="
					+ userId;
			sql2 = "select q.*,u.userNickName from question q,user u where q.questionUserId=u.userId and q.questionUserId="
					+ userId
					+ " order by questionTime desc limit "
					+ (currentPage - 1) * pageSize + "," + pageSize;
			sql3 = "select count(*) from question_answer where answerUId="
					+ userId;
			int mysubmit = getCount(sql1);
			int myanswer = getCount(sql3);
			page.myanswer = myanswer;
			page.mysubmit = mysubmit;
			page.count = mysubmit;
		} else {
			sql1 = "select count(*) from question where questionUserId="
					+ userId;
			sql2 = "select q.*,u.userNickName from question q,user u,question_answer qa where qa.questionId=q.questionId and q.questionUserId=u.userId and qa.answerUId="
					+ userId
					+ " order by qa.answerTime desc limit "
					+ (currentPage - 1) * pageSize + "," + pageSize;
			sql3 = "select count(*) from question_answer where answerUId="
					+ userId;
			int mysubmit = getCount(sql1);
			int myanswer = getCount(sql3);
			page.myanswer = myanswer;
			page.mysubmit = mysubmit;
			page.count = myanswer;
		}
		page.totalPage = page.count % pageSize == 0 ? page.count / pageSize
				: page.count / pageSize + 1;
		ArrayList<Question> list = new ArrayList<Question>();
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql2);
			rs = ps.executeQuery();
			while (rs.next()) {
				Question q = new Question();
				q.questionAnswerNum = rs.getInt("questionAnswerNum");
				q.questionContent = rs.getString("questionContent");
				String desc = q.questionContent;
				desc = desc.replaceAll("<[^>]*>", "");
				if (desc.length() >= 100) {
					q.questionDesc = desc.substring(0, 100);
				} else {
					q.questionDesc = desc;
				}
				q.questionDigged = rs.getInt("questionDigged");
				q.questionId = rs.getInt("questionId");
				String tag = rs.getString("questionTag");
				q.questionTag = tag.split(",");
				q.questionPoint = rs.getInt("questionPoint");
				q.questionTime = rs.getString("questionTime");
				q.questionTitle = rs.getString("questionTitle");
				q.questionUserId = rs.getInt("questionUserId");
				q.questionUserName = rs.getString("userNickName");
				q.state = rs.getInt("state");
				list.add(q);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.list = list;
		page.currentPage = currentPage;
		return page;
	}

	public String[] getQuestionDigged(int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String dig[] = new String[2];
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select diggedIds,qa_diggedIds from question_userinfo where userId="
							+ userId);
			rs = ps.executeQuery();
			while (rs.next()) {
				dig[0] = rs.getString("diggedIds");
				dig[1] = rs.getString("qa_diggedIds");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return dig;
	}

	public boolean digQuestion(int userId, int qId, String action) {
		Connection conn = null;
		Statement ps = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			if (action.equals("1")) {
				ps.addBatch("update question set questionDigged=questionDigged+1 where questionId="
						+ qId);
				ps.addBatch("update question_userinfo set diggedIds=concat(diggedIds,'"
						+ qId + ",') where userId=" + userId);
			} else {
				ps.addBatch("update question_answer set answerDigged=answerDigged+1 where answerId="
						+ qId);
				ps.addBatch("update question_userinfo set qa_diggedIds=concat(qa_diggedIds,'"
						+ qId + ",') where userId=" + userId);
			}
			ps.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return true;
	}

	public HashMap<String, Object> getQuestionById(int questionId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Question q = new Question();
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select q.*,u.userNickName from question q,user u where q.questionUserId=u.userId and questionId="
							+ questionId);
			rs = ps.executeQuery();
			if (rs.next()) {
				q.questionAnswerNum = rs.getInt("questionAnswerNum");
				q.questionBestAnswerId = rs.getInt("questionBestAnswerId");
				q.questionContent = rs.getString("questionContent");
				q.questionDigged = rs.getInt("questionDigged");
				q.questionId = rs.getInt("questionId");
				q.questionPoint = rs.getInt("questionPoint");
				q.questionTag = rs.getString("questionTag").split(",");
				q.questionTime = rs.getString("questionTime");
				q.questionTitle = rs.getString("questionTitle");
				q.questionUserId = rs.getInt("questionUserId");
				q.questionUserName = rs.getString("userNickName");
				q.state = rs.getInt("state");
			}
			ps.close();
			map.put("question", q);
			ps = conn
					.prepareStatement("select qa.*,u.userNickName,u.userIcon from question_answer qa,user u where qa.answerUId=u.userId and qa.questionId="
							+ questionId + " order by answerTime desc");
			rs = ps.executeQuery();
			ArrayList<QuestionAnswer> list = new ArrayList<QuestionAnswer>();
			String answerIds = "";
			while (rs.next()) {
				QuestionAnswer qa = new QuestionAnswer();
				qa.answerComNum = rs.getInt("answerComNum");
				qa.answerContent = rs.getString("answerContent");
				qa.answerId = rs.getInt("answerId");
				qa.answerTime = rs.getString("answerTime");
				qa.answerUId = rs.getInt("answerUId");
				qa.isBest = rs.getInt("isBest");
				qa.questionId = rs.getInt("questionId");
				qa.answerDigged = rs.getInt("answerDigged");
				qa.userIcon = rs.getString("userIcon");
				qa.userNickName = rs.getString("userNickName");
				answerIds += qa.answerId + ",";
				list.add(qa);
			}
			map.put("answerlist", list);
			ps.close();
			if (!StringUtils.isNullOrEmpty(answerIds)) {
				ps = conn
						.prepareStatement("select qc.*,u.userNickName,u.userIcon from question_comment qc,user u where qc.answerId in ("
								+ answerIds.substring(0, answerIds.length() - 1)
								+ ") and qc.commentUserId=u.userId order by commentTime desc");
				rs = ps.executeQuery();
				while (rs.next()) {
					QuestionComment qc = new QuestionComment();
					qc.answerId = rs.getInt("answerId");
					qc.commentContent = rs.getString("commentContent");
					qc.commentId = rs.getInt("commentId");
					qc.commentTime = rs.getString("commentTime");
					qc.commentUserId = rs.getInt("commentUserId");
					qc.userIcon = rs.getString("userIcon");
					qc.userName = rs.getString("userNickName");

					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).answerId == qc.answerId) {
							list.get(i).list.add(qc);
							break;
						}
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return map;
	}

	public boolean answerQuestion(int qId, int userId, String content) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "insert into question_answer(answerContent,answerUId,answerTime,questionId) values(?,?,?,?)";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, content);
			ps.setObject(2, userId);
			ps.setObject(3, Utils.getCurrentTime());
			ps.setObject(4, qId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public String getQuestionComment(int answerId) {
		JSONObject json = new JSONObject();
		JSONArray arr = new JSONArray();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select qc.*,u.userIcon,u.userNickName from question_comment qc,user u where qc.commentUserId=u.userId and qc.answerId="
							+ answerId + " order by commentTime desc");
			rs = ps.executeQuery();
			while (rs.next()) {
				JSONObject obj = new JSONObject();
				obj.put("commentId", rs.getInt("commentId"));
				obj.put("commentUserId", rs.getInt("commentUserId"));
				obj.put("commentContent", rs.getString("commentContent"));
				obj.put("commentTime", rs.getString("commentTime"));
				obj.put("answerId", rs.getInt("answerId"));
				obj.put("userIcon", rs.getString("userIcon"));
				obj.put("userNickName", rs.getString("userNickName"));
				arr.add(obj);
			}
			json.put("list", arr);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return json.toString();
	}

	public boolean replyAnswer(String con, int userId, int answerId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "insert into question_comment(commentContent,commentUserId,commentTime,answerId) values(?,?,?,?)";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, con);
			ps.setObject(2, userId);
			ps.setObject(3, Utils.getCurrentTime());
			ps.setObject(4, answerId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public boolean acceptAnswer(int questionId, int answerId) {
		Connection conn = null;
		Statement ps = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			ps.addBatch("update question_answer set isBest=1 where answerId="
					+ answerId);
			ps.addBatch("update question set state=1 ,questionBestAnswerId="
					+ answerId + " where questionId=" + questionId);
			ps.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return true;
	}

	public String editQuestion(int questionId) {
		JSONObject obj = new JSONObject();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select * from question where questionId="
							+ questionId);
			rs = ps.executeQuery();
			while (rs.next()) {
				obj.put("articleId", questionId + "");
				obj.put("title", rs.getString("questionTitle"));
				obj.put("content", rs.getString("questionContent"));
				obj.put("tag2", rs.getString("questionTag"));
				obj.put("point", rs.getInt("questionPoint"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return obj.toString();
	}

	public boolean updateQuestion(int questionId, String content, String title,
			String tag, String point) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("update question set questionContent=?,questionTitle=?,questionTag=?,questionPoint=? where questionId=?");
			ps.setObject(1, content);
			ps.setObject(2, title);
			ps.setObject(3, tag);
			if (StringUtils.isNullOrEmpty(point)) {
				ps.setObject(4, 0);
			} else {
				ps.setObject(4, Integer.parseInt(point));
			}
			ps.setObject(5, questionId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public SearchResultPage queryQuestion(String content, String currentPageStr) {
		SearchResultPage page = new SearchResultPage();
		ArrayList<SearchResult> list = new ArrayList<SearchResult>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int count = getCount("select count(*) from question where questionTitle like '%"
				+ content + "%' or questionTag like '%" + content + "%'");
		int pageSize = 15;
		int totalPage = (int) Math.ceil((double) count / (double) pageSize);
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select q.*,userNickName from question q,user u where questionUserId=userId and (questionTitle like '%"
							+ content
							+ "%' or questionTag like '%"
							+ content
							+ "%') order by questionDigged desc limit "
							+ (currentPage - 1) * pageSize + "," + pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				SearchResult sr = new SearchResult();
				sr.reCreatOn = rs.getString("questionTime");
				sr.reEditor = rs.getString("userNickName");
				sr.reEditorId = rs.getInt("questionUserId");
				sr.reHitNum = rs.getInt("questionDigged");
				sr.reId = rs.getInt("questionId");
				sr.reTitle = rs.getString("questionTitle");
				list.add(sr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.list = list;
		page.currentPage = currentPage;
		page.count = count;
		page.totalPage = totalPage;
		return page;
	}

	public ArrayList<Data> getData(ResultSet rs) {
		ArrayList<Data> list = new ArrayList<Data>();
		try {
			while (rs.next()) {
				Data data = new Data();
				data.dataId = rs.getInt("dataId");
				data.dataName = rs.getString("dataName");
				data.dataType_CN = rs.getString("dataType_CN");
				list.add(data);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public DataList getDataList() {
		DataList list = new DataList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql1 = "select dataId,dataName,dataType_CN from data where isDelete=0 order by dataDownloadNum desc limit 0,26";
		String sql2 = "select dataId,dataName,dataType_CN from data where dataType=? and isDelete=0 order by dataDownloadNum desc limit 0,12";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql1);
			rs = ps.executeQuery();
			list.recommendList = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "development_technology");
			rs = ps.executeQuery();
			list.development_technology = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "course_resource");
			rs = ps.executeQuery();
			list.course_resource = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "mobile_development");
			rs = ps.executeQuery();
			list.mobile_development = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "network_technology");
			rs = ps.executeQuery();
			list.network_technology = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "operating_system");
			rs = ps.executeQuery();
			list.operating_system = getData(rs);
			ps.close();

			ps = conn.prepareStatement(sql2);
			ps.setObject(1, "industry");
			rs = ps.executeQuery();
			list.industry = getData(rs);
			ps.close();

			list.course_resource_num = getCount("select count(*) from data where dataType='course_resource' and isDelete=0");
			list.development_technology_num = getCount("select count(*) from data where dataType='development_technology' and isDelete=0");
			list.industry_num = getCount("select count(*) from data where dataType='industry' and isDelete=0");
			list.mobile_development_num = getCount("select count(*) from data where dataType='mobile_development' and isDelete=0");
			list.network_technology_num = getCount("select count(*) from data where dataType='network_technology' and isDelete=0");
			list.operating_system_num = getCount("select count(*) from data where dataType='operating_system' and isDelete=0");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return list;
	}

	public DataPage getDataPage(String type, String currentPageStr,
			String orderby) {
		DataPage page = new DataPage();
		ArrayList<Data> list = new ArrayList<Data>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		int count = getCount("select count(*) from data where dataType='"
				+ type + "' and isDelete=0");
		int pageSize = 10;
		int totalPage = (int) Math.ceil((double) count / (double) pageSize);
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select d.*,userNickName from data d,user u where d.dataUId=u.userId and isDelete=0 and dataType='"
							+ type
							+ "' order by "
							+ orderby
							+ " desc limit "
							+ (currentPage - 1) * pageSize + "," + pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				Data data = new Data();
				data.dataCategory = rs.getString("dataCategory");
				data.dataDesc = rs.getString("dataDesc");
				data.dataDownloadNum = rs.getInt("dataDownloadNum");
				data.dataId = rs.getInt("dataId");
				data.dataName = rs.getString("dataName");
				data.dataPoint = rs.getInt("dataPoint");
				data.dataTag = rs.getString("dataTag").split(",");
				data.dataTime = rs.getString("dataTime").substring(0, 10);
				data.dataUId = rs.getInt("dataUId");
				data.dataUserName = rs.getString("userNickName");
				list.add(data);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count = count;
		page.currentPage = currentPage;
		page.list = list;
		page.totalPage = totalPage;
		return page;
	}

	public Data getDataById(int dataId) {
		Data data = new Data();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select d.*,userNickName from data d,user where d.dataUId=user.userId and dataId="
							+ dataId);
			rs = ps.executeQuery();
			if (rs.next()) {
				data.dataCategory = rs.getString("dataCategory");
				data.dataDesc = rs.getString("dataDesc");
				data.dataDownloadNum = rs.getInt("dataDownloadNum");
				data.dataId = dataId;
				data.dataName = rs.getString("dataName");
				data.dataPoint = rs.getInt("dataPoint");
				data.dataTag = rs.getString("dataTag").split(",");
				data.dataTime = rs.getString("dataTime").substring(0, 10);
				data.dataType_CN = rs.getString("dataType_CN");
				data.dataType = rs.getString("dataType");
				data.dataUId = rs.getInt("dataUId");
				data.dataUserName = rs.getString("userNickName");
				data.dataCollNum = rs.getInt("dataCollNum");
				data.dataComNum = rs.getInt("dataComNum");
				data.dataSize = rs.getString("dataSize");
				data.realDataName = rs.getString("realDataName");
				data.storeDataName = rs.getString("storeDataName");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return data;
	}

	public DataCommentPage loadDataComment(int dataId, String currentPageStr) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		DataCommentPage page = new DataCommentPage();
		ArrayList<DataComment> list = new ArrayList<DataComment>();
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		int count = getCount("select count(*) from data_comment where dataId="
				+ dataId);
		int pageSize = 10;
		int totalPage = (int) Math.ceil((double) count / (double) pageSize);
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select dc.*,userNickName,userIcon from data_comment dc,user where dc.comUserId=user.userId and dataId="
							+ dataId
							+ " order by comTime desc limit "
							+ (currentPage - 1) * pageSize + "," + pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				DataComment dc = new DataComment();
				dc.comContent = rs.getString("comContent");
				dc.comId = rs.getInt("comId");
				dc.comUserId = rs.getInt("comUserId");
				dc.comUserName = rs.getString("userNickName");
				dc.comTime = rs.getString("comTime").substring(0, 10);
				dc.dataId = rs.getInt("dataId");
				dc.comUserIcon = rs.getString("userIcon");
				list.add(dc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count = count;
		page.currentPage = currentPage;
		page.list = list;
		page.totalPage = totalPage;
		return page;
	}

	public String getUserDownload(int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String str = "";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select userDownload from data_userinfo where userId="
							+ userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				str += rs.getString("userDownload");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return str;
	}

	public boolean submitDataComment(int dataId, String content, int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag = 0;
		String sql = "insert data_comment(comContent,comUserId,dataId,comTime)values(?,?,?,?)";
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			ps.setObject(1, content);
			ps.setObject(2, userId);
			ps.setObject(3, dataId);
			ps.setObject(4, Utils.getCurrentTime());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if (flag == 1)
			return true;
		return false;
	}

	public boolean isDownloaded(int dataId, int userId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int flag = 0;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn
					.prepareStatement("select count(*) from data_comment where dataId="
							+ dataId + " and comUserId=" + userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				flag = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		if (flag >= 1)
			return true;
		return false;
	}

	public boolean isContain(String[] arr, String str) {
		for (String s : arr) {
			if (s.equals(str)) {
				return true;
			}
		}
		return false;
	}

	public boolean download(int dataId, int point, int userId, int dataUserId) {
		Connection conn = null;
		Statement ps = null;
		String did = getUserDownload(userId);
		String[] ids = did.split(",");
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.createStatement();
			if (isContain(ids, dataId + "")) {
				ps.addBatch("update user set userPoint=userPoint-" + point
						+ " where userId=" + userId);
				ps.addBatch("update user set userPoint=userPoint+" + point
						+ " where userId=" + dataUserId);
			} else {
				ps.addBatch("update user set userPoint=userPoint-" + point
						+ " where userId=" + userId);
				ps.addBatch("update data_userinfo set userDownload=concat(userDownload,'"
						+ dataId + ",') where userId=" + userId);
				ps.addBatch("update user set userPoint=userPoint+" + point
						+ " where userId=" + dataUserId);
			}
			ps.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		return true;
	}

	public boolean uploadFile(int userId, int point, String dataName,
			String type, String category, String desc, String tag,
			String dataSize, String realDataName, String storeDataName) {
		Connection conn = null;
		PreparedStatement ps = null;
		int flag=0;
		String sql = "insert into data(dataName,dataDesc,dataType,dataType_CN,dataPoint,dataUId,dataTime,dataTag,dataCategory,dataSize,realDataName,storeDataName) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement(sql);
			ps.setObject(1, dataName);
			ps.setObject(2, desc);
			ps.setObject(3, type);
			ps.setObject(4, Utils.getDataRelativeName(type));
			ps.setObject(5, point);
			ps.setObject(6, userId);
			ps.setObject(7, Utils.getCurrentTime());
			ps.setObject(8, tag);
			ps.setObject(9, category);
			ps.setObject(10, dataSize);
			ps.setObject(11, realDataName);
			ps.setObject(12, storeDataName);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, null);
		}
		if(flag==1) return true;
		return false;
	}
	
	
	public DataPage getMyData(int userId,String type,String currentPageStr){
		DataPage page=new DataPage();
		Connection conn = null;
		PreparedStatement ps = null;
		ArrayList<Data> list=new ArrayList<Data>();
		ResultSet rs=null;
		String sql1="",sql2="";
		int pageSize=10;
		int currentPage=1;
		if(!StringUtils.isNullOrEmpty(currentPageStr)){
			currentPage=Integer.parseInt(currentPageStr);
		}
		if(type.equals("in")){
			sql1+="select count(*) from data where dataUId="+userId+" and isDelete=0";
			page.count=getCount(sql1);
			page.totalPage=(int) Math.ceil((double)page.count/pageSize);
			sql2+="select dataId,dataName,dataDesc,dataPoint,dataTag,dataTime,dataDownloadNum,userNickName from data,user where dataUId=userId and dataUId="+userId+" and isDelete=0 order by dataTime desc limit "+(currentPage-1)*pageSize+","+pageSize;
		}else{
			String id=getUserDownload(userId);
			String[] ids=id.substring(2, id.length()).split(",");
			int length=ids.length;
			if(length==1&&ids[0].equals("")){
				return page;
			}
			for(int i=0;i<length/2;i++){
				String flag=ids[i];
				ids[i]=ids[length-1-i];
				ids[length-i-1]=flag;
			}
			StringBuilder sb=new StringBuilder("(");
			int totalPage=(int) Math.ceil((double)length/(double)pageSize);
			if(currentPage<totalPage){
				for(int i=(currentPage-1)*pageSize;i<pageSize*currentPage;i++){
					sb.append(ids[i]+",");
				}
			}else{
				for(int i=(currentPage-1)*pageSize;i<length;i++){
					sb.append(ids[i]+",");
				}
			}
			System.out.println(length);
			page.count=length;
			page.totalPage=totalPage;
			sql2+="select dataId,dataName,dataDesc,dataPoint,dataTag,dataTime,dataDownloadNum,userNickName,userId from data,user where dataId in "+sb.substring(0, sb.length()-1)+") and dataUId=userId and isDelete=0";
		}
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement(sql2);
			rs=ps.executeQuery();
			while(rs.next()){
				Data data=new Data();
				String desc=rs.getString("dataDesc");
				if(desc.length()>100){
					data.dataDesc=desc.substring(0, 100);
				}else{
					data.dataDesc=desc;
				}
				data.dataDownloadNum=rs.getInt("dataDownloadNum");
				data.dataId=rs.getInt("dataId");
				data.dataName=rs.getString("dataName");
				data.dataPoint=rs.getInt("dataPoint");
				data.dataTag=rs.getString("dataTag").split(",");
				data.dataTime=rs.getString("dataTime").substring(0,10);
				data.dataUserName=rs.getString("userNickName");
				if(type.equals("out")){
					data.dataUId=rs.getInt("userId");
				}
				list.add(data);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.list=list;
		page.currentPage=currentPage;
		return page;	
	}
	
	public boolean deleteData(int dataId){
		Connection conn=null;
		PreparedStatement ps=null;
		int flag=0;
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("update data set isDelete=1 where dataId="+dataId);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, null);
		}
		if(flag==1) return true;
		return false;
	}
	
	public SearchResultPage queryData(String content,String currentPageStr){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs=null;
		SearchResultPage page=new SearchResultPage();
		int pageSize=15;
		int currentPage=1;
		if(!StringUtils.isNullOrEmpty(currentPageStr)){
			currentPage=Integer.parseInt(currentPageStr);
		}
		int count=getCount("select count(*) from data where (dataName like '%"+content+"%' or dataTag like '%"+content+"%' or dataDesc like '%"+content+"%' or dataType_CN like '%"+content+"%') and isDelete=0");
		int totalPage=(int) Math.ceil((double)count/pageSize);
		ArrayList<SearchResult> list=new ArrayList<SearchResult>();
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select data.*,userNickName from data,user where (dataName like '%"+content+"%' or dataTag like '%"+content+"%' or dataDesc like '%"+content+"%' or dataType_CN like '%"+content+"%') and dataUId=userId and isDelete=0 order by dataDownloadNum desc limit "+(currentPage-1)*pageSize+","+pageSize);
			rs=ps.executeQuery();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reCreatOn=rs.getString("dataTime").substring(0,10);
				sr.reEditorId=rs.getInt("dataUId");
				sr.reEditor=rs.getString("userNickName");
				sr.reHitNum=rs.getInt("dataDownloadNum");
				sr.reId=rs.getInt("dataId");
				sr.reTitle=rs.getString("dataName");
				list.add(sr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		page.count=count;
		page.currentPage=currentPage;
		page.list=list;
		page.totalPage=totalPage;
		return page;
	}
	
	public String getUserNickName(int userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs=null;
		String name="";
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select userNickName from user where userId="+userId);
			rs=ps.executeQuery();
			if(rs.next()){
				name+=rs.getString(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return name;
	}
	
	public User getMyInfo(int userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs=null;
		User user=new User();
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select * from user where userId="+userId);
			rs=ps.executeQuery();
			if(rs.next()){
				user.userName=rs.getString("userName");
				user.userBrief=rs.getString("userBrief");
				user.userIcon=rs.getString("userIcon");
				user.userNickName=rs.getString("userNickName");
				user.userPoint=rs.getInt("userPoint");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return user;
	}
	
	public boolean updateInfo(String userNickName,String userBrief,int userId,String file){
		Connection conn = null;
		PreparedStatement ps = null;
		int flag=0;
		String sql="";
		if(file.equals("")){
			sql+="update user set userNickName='"+userNickName+"',userBrief='"+userBrief+"' where userId="+userId;
		}else{
			sql+="update user set userNickName='"+userNickName+"',userBrief='"+userBrief+"',userIcon='"+file+"' where userId="+userId;
		}
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement(sql);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, null);
		}
		if(flag==1) return true;
		return false;
	}
	
	public String loadDynamicByPage(int userId,String currentPageStr) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONObject json=new JSONObject();
		JSONArray arr=new JSONArray();
		String sql = "";
		sql = "select count(*) from blog_comment where isDelete=0 and comUserId=" + userId;
		int count = getCount(sql);
		int currentPage = 1;
		if (!StringUtils.isNullOrEmpty(currentPageStr)) {
			currentPage = Integer.parseInt(currentPageStr);
		}
		ArrayList<BlogComment> list = new ArrayList<BlogComment>();
		if (count == 0) {
			json.put("count", 0);
			json.put("currentPage", currentPage);
			arr.addAll(list);
			json.put("list", arr);
			return json.toString();
		}
		int pageSize = 10;
		int totalPage = count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1;
		sql = "select a.time,a.comId,a.blogId,a.comContent,a.comUserId,b.blogName,b.userId,c.userNickName from blog_comment a,blog b,user c where a.blogId=b.blogId and a.comUserId=c.userId and a.isDelete=0 and a.comUserId = "
				+ userId
				+ " order by time desc limit "
				+ (currentPage - 1)
				* pageSize + "," + pageSize;
		try {
			conn = DBAccess.getInstance().getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				BlogComment comment = new BlogComment();
				comment.blogId = rs.getInt("blogId");
				comment.blogName = rs.getString("blogName");
				comment.comContent = rs.getString("comContent");
				comment.comId = rs.getInt("comId");
				comment.comUserName = rs.getString("userNickName");
				comment.comUserId = rs.getInt("comUserId");
				comment.blogUserId = rs.getInt("userId");
				comment.time = rs.getString("time");
				list.add(comment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(conn, ps, rs);
		}
		
		arr.addAll(list);
		json.put("count", count);
		json.put("currentPage", currentPage);
		json.put("totalPage", totalPage);
		json.put("list", arr);
		return json.toString();
	}
	
	public String loadMyFocusDynamic(int userId,String currentPageStr){
		int currentPage=1;
		int pageSize=10;
		if(!StringUtils.isNullOrEmpty(currentPageStr)){
			currentPage=Integer.parseInt(currentPageStr);
		}
		JSONObject json=new JSONObject();
		JSONArray arr=new JSONArray();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ArrayList<BlogComment> list = new ArrayList<BlogComment>();
		String attention="";
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select attention from blog_userinfo where userId="+userId);
			rs=ps.executeQuery();
			if(rs.next()){
				attention=rs.getString("attention");
			}
			if(attention.startsWith("0,")){
				attention=attention.substring(2, attention.length());
			}
			json.put("currentPage", currentPage);
			if(attention.equals("")){
				DBAccess.getInstance().close(conn, ps, rs);
				arr.addAll(list);
				json.put("list", arr);
				return json.toString();
			}
			int count=getCount("select count(*) from blog_comment where isDelete=0 and comUserId in ("+attention.substring(0, attention.length()-1)+")");
			ps=conn.prepareStatement("select a.time,a.comId,a.blogId,a.comContent,a.comUserId,b.blogName,b.userId,c.userNickName from blog_comment a,blog b,user c where a.blogId=b.blogId and a.comUserId=c.userId and a.isDelete=0 and a.comUserId in ("+attention.substring(0, attention.length()-1)+") order by time desc limit "
					+ (currentPage - 1)
					* pageSize + "," + pageSize);
			rs=ps.executeQuery();
			while(rs.next()){
				BlogComment comment = new BlogComment();
				comment.blogId = rs.getInt("blogId");
				comment.blogName = rs.getString("blogName");
				comment.comContent = rs.getString("comContent");
				comment.comId = rs.getInt("comId");
				comment.comUserName = rs.getString("userNickName");
				comment.comUserId = rs.getInt("comUserId");
				comment.blogUserId = rs.getInt("userId");
				comment.time = rs.getString("time");
				list.add(comment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		arr.addAll(list);
		json.put("list", arr);
		return json.toString();
	}
	
	public String getMyBlog(int userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONObject json=new JSONObject();
		JSONArray arr=new JSONArray();
		ArrayList<SearchResult> list=new ArrayList<SearchResult>();
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select blogId,blogName,readNum,commentNum from blog where state=1 and isDelete=0 and userId="+userId+" order by readNum desc limit 0,5");
			rs=ps.executeQuery();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.comNum=rs.getInt("commentNum");
				sr.reEditorId=userId;
				sr.reHitNum=rs.getInt("readNum");
				sr.reId=rs.getInt("blogId");
				sr.reTitle=rs.getString("blogName");
				list.add(sr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		arr.addAll(list);
		json.put("list", arr);
		return json.toString();
	}
	
	public String getMyBBS(int userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONObject json=new JSONObject();
		JSONArray arr=new JSONArray();
		ArrayList<SearchResult> list=new ArrayList<SearchResult>();
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select ID,Title,Hit from bbs_topic where UserID="+userId+" order by Hit desc limit 0,5");
			rs=ps.executeQuery();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reHitNum=rs.getInt("Hit");
				sr.reId=rs.getInt("ID");
				sr.reTitle=rs.getString("Title");
				list.add(sr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		arr.addAll(list);
		json.put("list", arr);
		return json.toString();
	}
	
	public String getMyData(int userId){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		JSONObject json=new JSONObject();
		JSONArray arr=new JSONArray();
		ArrayList<SearchResult> list=new ArrayList<SearchResult>();
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select dataId,dataName,dataDownloadNum,dataComNum,dataType_CN from data where isDelete=0 and dataUId="+userId+" order by dataDownloadNum desc limit 0,5");
			rs=ps.executeQuery();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.comNum=rs.getInt("dataComNum");
				sr.reHitNum=rs.getInt("dataDownloadNum");
				sr.reId=rs.getInt("dataId");
				sr.reTitle="["+rs.getString("dataType_CN")+"]"+rs.getString("dataName");
				list.add(sr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		arr.addAll(list);
		json.put("list", arr);
		return json.toString();
	}
	
	public HashMap<String, ArrayList<SearchResult>> getHomeData(){
		HashMap<String, ArrayList<SearchResult>> map=new HashMap<String, ArrayList<SearchResult>>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn=DBAccess.getInstance().getConn();
			ps=conn.prepareStatement("select blogId,blogName,userId from blog where isDelete=0 and state=1 order by time desc limit 0,10");
			rs=ps.executeQuery();
			ArrayList<SearchResult> blog=new ArrayList<SearchResult>();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reEditorId=rs.getInt("userId");
				sr.reId=rs.getInt("blogId");
				sr.reTitle=rs.getString("blogName");
				blog.add(sr);
			}
			map.put("blog", blog);
			ps.close();
			
			ps=conn.prepareStatement("select ID,Title from bbs_topic order by CreatedOn desc limit 0,10");
			rs=ps.executeQuery();
			ArrayList<SearchResult> bbs=new ArrayList<SearchResult>();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reId=rs.getInt("ID");
				sr.reTitle=rs.getString("Title");
				bbs.add(sr);
			}
			map.put("bbs", bbs);
			ps.close();
			
			ps=conn.prepareStatement("select questionId,questionTitle from question where state=0 order by questionTime desc limit 0,10");
			rs=ps.executeQuery();
			ArrayList<SearchResult> question=new ArrayList<SearchResult>();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reId=rs.getInt("questionId");
				sr.reTitle=rs.getString("questionTitle");
				question.add(sr);
			}
			map.put("question", question);
			ps.close();
			
			ps=conn.prepareStatement("select dataId,dataName,dataType_CN from data where isDelete=0 order by dataTime desc limit 0,10");
			rs=ps.executeQuery();
			ArrayList<SearchResult> data=new ArrayList<SearchResult>();
			while(rs.next()){
				SearchResult sr=new SearchResult();
				sr.reId=rs.getInt("dataId");
				sr.reTitle="["+rs.getString("dataType_CN")+"]"+rs.getString("dataName");
				data.add(sr);
			}
			map.put("data", data);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBAccess.getInstance().close(conn, ps, rs);
		}
		return map;
	}
}
